package com.wiscamp.ninechapters.problems.math;

import com.wiscamp.ninechapters.common.core.ParseHelper;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 不等式的解
 */
@Data
public class LatexInequalitySolution implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(LatexInequalitySolution.class);

    /**
     * 变量
     */
    private String variable;

    /**
     * 方程解的值
     */
    private BigDecimal value = new BigDecimal(0);

    /**
     * 方程解序号
     */
    private int index = 0;

    /**
     * 是否是方程解的值的近似数 （待定）
     */
    private boolean approximateNumber = false;

    /**
     * 除等号外的比较操作符
     */
    private String compareOperator;

    public LatexInequalitySolution(String variable, BigDecimal value, int index, boolean approximateNumber, String compareOperator) {
        this.approximateNumber = approximateNumber;
        this.index = index;
        this.value = value;
        this.variable = variable;
        this.compareOperator = compareOperator;
    }

    public LatexInequalitySolution(String content) {

    }

    public String getLatex() {
        if (this.index >= 1)
            return String.format("%s_%d %s %s", variable, index, compareOperator, value.stripTrailingZeros());
        else
            return String.format("%s %s %s", variable, compareOperator, value.stripTrailingZeros());
    }

    public static LatexInequalitySolution parseInequalitySolution(String latexText) {
        var compareOperator = getCompareOperator(latexText);
        var tempStr = latexText.replace(" ", "").trim();
        var variableText = "";
        var indexText = "";
        var valueText = "";
        if (tempStr.contains("_")) {
            int leftIndex = tempStr.indexOf("_");
            int rightIndex = tempStr.indexOf(compareOperator);
            variableText = tempStr.substring(0, leftIndex);
            indexText = tempStr.substring(leftIndex + 1, rightIndex);
            valueText = tempStr.substring(rightIndex + compareOperator.length());
            logger.debug("parsing inequality solution. Variable: {} Index: {} Value: {}", variableText, indexText, valueText);
        } else {
            int leftIndex = tempStr.indexOf(compareOperator);
            variableText = tempStr.substring(0, leftIndex);
            indexText = "0";
            valueText = tempStr.substring(leftIndex + compareOperator.length());
            logger.debug("parsing inequality solution. Variable: {} Index: {} Value: {}", variableText, indexText, valueText);
        }
        int index = ParseHelper.parseInteger(indexText);
        BigDecimal value = ParseHelper.parseBigDecimal(valueText);
        return new LatexInequalitySolution(variableText, value, index, false, compareOperator);
    }

    public static String getCompareOperator(String latexText) {
        if (latexText.contains("<>")) return "<>";
        if (latexText.contains(">=")) return ">=";
        if (latexText.contains("<=")) return "<=";
        if (latexText.contains("\\ne")) return "\\ne";
        if (latexText.contains("\\ge")) return "\\ge";
        if (latexText.contains("\\le")) return "\\le";
        if (latexText.contains(">")) return ">";
        if (latexText.contains("<")) return "<";
        if (latexText.contains("=")) return "=";

        // 不合法的答案内容
        return "";
    }
}
